<template>
  <!-- 单选树状弹出层 -->
  <el-dialog
    title="分配权限"
    :visible="treeVisible"
    width="30%"
    @close="handleClose"
  >
    <el-tree
      ref="noses"
      :data="treeProps"
      :props="defaultProps"
      show-checkbox
      node-key="id"
      :default-checked-keys="lastId"
      :default-expand-all="true"
    />
    <!-- @check-change="handleNodeClick" -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:treeVisible',false)">取 消</el-button>
      <el-button type="primary" @click="permissionAddition">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRoleIdRids } from '@/api/jurisdiction.js'

export default {
  name: 'Shishi',
  props: {
    // 弹出判断
    treeVisible: {
      type: Boolean,
      required: true
    },
    // 树状图数据
    treeProps: {
      type: Array,
      required: true
    },
    // 这一项的id
    treePropsId: {
      type: Number,
      required: true
    },
    // 选中的id
    lastId: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      rids: []
    }
  },

  created() {
    console.log(this.treeProps)
  },

  methods: {
    // 定义退出
    handleClose() {
      this.$emit('clear-radio')
      this.$emit('update:treeVisible', false)
    },
    // 点击获取的参数
    handleNodeClick(data) {
      // console.log(res)
      // console.log(data)
      // this.rids.push(data.id)
      // console.log(this.rids)
    },
    // 确定添加
    async permissionAddition() {
      const selectedOkData = this.$refs.noses.getCheckedNodes(false, true)
      const selectedData = selectedOkData.map(item => item.id).join()
      // console.log(this.$refs.noses.getCheckedKeys())
      try {
        await addRoleIdRids(this.treePropsId, { rids: selectedData })
        this.$emit('addRole')
        this.$message.success('分配权限成功')
        this.$emit('clear-radio')
      } catch (err) {
        this.$message.error('错了哦，添加失败')
      }
      this.$emit('update:treeVisible', false)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
